#include "stdafx.h"
#include "ZPoolAllocator.h"
#include <assert.h>

namespace	Z	{

//------------------------------------------------------------------------------------------------------------------------
template<>	PoolAllocator*	Singleton<PoolAllocator>::msSingleton	= nullptr;

PoolAllocator::PoolAllocator()
{
	for(size_t i = 0; i < mPools.size(); ++i)	{
		mPools[i]	= PoolPtr(new boost::pool<>(i + 1));
	}
}

PoolAllocator::~PoolAllocator()
{
}

void* PoolAllocator::allocate(size_t kSize)
{
	if(mPools.size() < kSize)
		return ::operator new(kSize);
	mutex_t::scoped_lock	lock(mLock);
	return mPools[kSize - 1]->malloc();
}

void PoolAllocator::deallocate(void* memory, size_t kSize)
{
	if(mPools.size() < kSize)
		return ::operator delete(memory);
	mutex_t::scoped_lock	lock(mLock);
	assert(mPools[kSize - 1]->is_from(memory));
	mPools[kSize - 1]->free(memory);
}

}